﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.MenuCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.MenuHandler
{
    public class AddRMHandler : IRequestHandler<AddRMCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RoleMenu> rmRep;
        private readonly IMapper mapper;

        public AddRMHandler(IBaseRepository<RoleMenu> rmRep, IMapper mapper)
        {
            this.rmRep = rmRep;
            this.mapper = mapper;
        }
        public async Task<APIResult<int>> Handle(AddRMCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var b = 0;
            foreach (var item in request.MenuIds)
            {
                var ur = new RoleMenu();
                ur.MenuId = item;
                ur.RoleId = request.RoleId;
                b = rmRep.AddAsync(ur).Result;
            }
            if (b > 0)
            {
                result.Code = APIEnum.Success;
                result.Msg = "添加成功";
                result.Data = b;

            }
            else
            {
                result.Code = APIEnum.Failed;
                result.Msg = "添加失败";
                result.Data = b;

            }
            return await Task.FromResult(result);


        }

    }
}
